<template>
    <div>
        <h3>优惠券添加</h3>
      
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>优惠券类型 <span>*</span></td>
                    <td>
                        <select v-model="add.DiscountType">
                            <option value="">请选择</option>
                            <option :value="1">代金券</option>
                            <option :value="2">折扣券</option>
                            <option :value="3">兑换券</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>优惠券标题<span>*</span></td>
                    <td>
                        <input type="text" v-model="add.Title">
                    </td>
                </tr>
                <tr>
                    <td>副标题</td>
                    <td>
                        <input type="text" v-model="add.subTitle">
                    </td>
                </tr>
                <tr>
                    <td>图片</td>
                    <td>
                        <input type="file" @change="img">
                        <img :src="add.Photo" alt="" width="100" height="100" v-if="add.Photo!=''">
                    </td>
                </tr>
                <tr>
                    <td>库存</td>
                    <td>
                        <input type="text" v-model="add.Count">
                    </td>
                </tr>
                <tr>
                    <td>限领数量</td>
                    <td>
                        <input type="text" v-model="add.PersonNumber" disabled>
                    </td>
                </tr>
                <tr>
                    <td>消费金额</td>
                    <td>
                        <input type="text" v-model="add.AmountMoney">
                    </td>
                </tr>
                <!-- 是否显示 -->
                <tr v-if="add.DiscountType==1">
                    <td>减免金额</td>
                    <td>
                        <input type="text" v-model="add.SubMoney" >
                    </td>
                </tr>
                   <!-- 是否显示 -->
                <tr v-if="add.DiscountType==2">
                    <td>折扣价</td>
                    <td>
                        <input type="text" v-model="add.ZheKou">
                    </td>
                </tr>
                <tr>
                    <td>有效期</td>
                    <td>
                        <input type="date" v-model="add.BeginTime">- <input type="date" v-model="add.EndTime">
                    </td>
                </tr>
                <tr>
                    <td>适用业务</td>
                    <td>
                       <input type="checkbox" name="yewu" value="订房" v-model="add.UserService">订房
                       <input type="checkbox" name="yewu" value="商场" v-model="add.UserService">商场
                       <input type="checkbox" name="yewu" value="购物" v-model="add.UserService">购物
                       <input type="checkbox" name="yewu" value="付款码" v-model="add.UserService">付款码
                    </td>
                </tr>
                <tr>
                    <td>使用须知</td>
                    <td>
                        <textarea cols="50" rows="5" v-model="add.UseDesc"></textarea>
                    </td>
                </tr>
                <tr>
                    <td>门店地址</td>
                    <td>
                       <select v-model="add.ShopId">
                        <option value="">请选择</option>
                        <option :value="item.Id" v-for="item in shop.list">{{ item.ShopName }}</option>
                       </select>
                    </td>
                </tr>
                <tr>
                    <td>服务电话</td>
                    <td>
                        <input type="text" maxlength="11" v-model="add.Phone">
                    </td>
                </tr>
                <tr>
                    <td>优惠券颜色</td>
                    <td>
                        <input type="color" v-model="add.Color">
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="button" value="添加" class="btn btn-info" @click="adddis">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import{ref,reactive,onMounted} from 'vue'
import {useRouter,useRoute} from 'vue-router'
import axios from 'axios';
import moment, { lang } from 'moment';
const router=useRouter();
const route=useRoute();

onMounted(()=>{
    showlist();
})

const img=(e:any)=>{
    let obj=e.target.files[0];
    let f=new FormData();
    f.append('file',obj);
    axios({
        method:'post',
        url:'/api/Imgs/UpdateImg',
        data:f,
    })

    .then(res=>{
        console.log(res);
        if(res.data=="不是图片")
    {
        alert("您上传的不是图片");
    }
    if(res.data=="大于2M"){
        alert("您上传大于2M");
        return;
    }
        add.Photo=res.data;

    })
    .catch(err=>{
        console.log(err);
    })

}


const add:any=reactive({
    
  "DiscountType": '',
  "Title": "",
  "subTitle": "",
  "Photo": "",
  "Count": '',
  "PersonNumber": 1,
  "AmountMoney": '',
  "SubMoney": 0,
  "ZheKou": 10,
  "BeginTime": "",
  "EndTime": "",
  "UserService": [],
  "UseDesc": "",
  "ShopId": '',
  "Phone": "",
  "Color": "#ff0000",
  
})
const adddis=()=>{
    //正整数
    if(isNaN(add.Count)==true){
        alert("优惠券库存只能是大于0的整数");
        return;
    }
    if(parseInt(add.Count)<0){
        alert("优惠券库存只能是大于0的整数");
        return;
    }
    if(add.SubMoney=="" && add.Discount==1){
        alert("减免金额不能是空值");
        return;
    }

    if(add.ZheKou=="" && add.Discount==2){
        alert("折扣不能是空值");
        return;
    }
    if((add.ZheKou<0 || add.ZheKou>9.9) &&add.Discount==2){
        alert("折扣只能是0-9.9折");
        return;
    }
    if(add.UserService.length==0){
        alert("使用业务必选");
        return;
    }
    if(add.shopId==""){
        alert("适用门店不能为空");
        return;
    }
    if(add.Phone==""){
        alert("电话号码不能为空");
        return;
    }
    


    add.UserService=add.UserService.toString();
    axios({
        url:'/api/Discount/DiscountAdd',
        method:'post',
        data:add,

    })
    .then(res=>{
        console.log(res);
        if(res.data==-1){
            alert("优惠券标题不能重复");
            return;
        }
        if(res.data>0){
            alert("添加成功");
        }
        else{
            alert("添加失败");
            return;
        }

    })
    .catch(err=>{
        console.log(err);
    })
}

let quertInfo1:any=reactive({
    pageIndex:1,
    pageSize:1000,
    shopName:'',
    date1:'',
});
let shop:any=reactive({
    list:[],
    totalCount:0,
    totalPage:0,
})
const showlist=()=>{
    axios({
        method:'get',
        url:'/api/Shop/GetShopList',
        params:
        quertInfo1,
        
    })
    .then(res=>{
        console.log(res);
        shop.list=res.data.list
        shop.totalCount=res.data.totalCount
        shop.totalPage=res.data.totalPage

    })
    .catch(err=>{
        console.log(err);
    })
}

</script>

<style scoped>
span{
    color: red;
}
</style>